package com.bsoft.gol.editor.controller;

import hcn.util.ResultCode;
import com.bsoft.gol.editor.utils.CommonUtils;

import hcn.util.BaseResponse;
import com.bsoft.gol.editor.editor.EditorSkipLogic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.bsoft.gol.editor.service.EditorSkipLogicApi;

import java.util.List;
import java.util.Map;

/**
 * @author zhangjie
 * @date 2019/6/21
 */
@RestController
@RequestMapping(value = "/api/auth/editorSkipLogic")
public class EditorSkipLogicController {

    @Autowired
    private EditorSkipLogicApi editorSkipLogicApi;

    /**
     * 跳题逻辑新增 包括批量
     * @param req
     * @return
     */
    @RequestMapping("/add")
    public BaseResponse<String> add(@RequestBody EditorSkipLogic req) {
        BaseResponse<String> response = new BaseResponse<>();
        // 删除该题目的所有跳题逻辑，再循环新增跳题逻辑
        if (CommonUtils.isNull(req.getTitleId())) {
            response.setCode(ResultCode.PARAMETERS_EMPTY);
            response.setMessage("传入参数不能为空");
            return response;
        }
        Map<String, Object> resMap = editorSkipLogicApi.saveBatch(req);

        if(!resMap.get("code").equals(ResultCode.SUCCESS)){
            response.setCode(ResultCode.ERROR);
            response.setMessage("操作失败");
        }
        return response;
    }

    /**
     * 跳题逻辑删除
     * @param req
     * @return
     */
    @RequestMapping("/delete")
    public BaseResponse<String> delete(@RequestBody EditorSkipLogic req) {
        BaseResponse<String> response = new BaseResponse<>();
        if (CommonUtils.isNull(req.getSkipId())) {
            response.setCode(ResultCode.PARAMETERS_EMPTY);
            response.setMessage("传入参数不能为空");
            return response;
        }
        editorSkipLogicApi.deleteSkipLogic(req);
        response.setMessage("删除成功");
        return response;
    }

    /**
     * 跳题逻辑列表
     * @param req
     * @return
     */
    @RequestMapping("/list")
    public BaseResponse<List<EditorSkipLogic>> list(@RequestBody EditorSkipLogic req) {
        BaseResponse<List<EditorSkipLogic>> response = new BaseResponse<List<EditorSkipLogic>>();
        if (CommonUtils.isNull(req.getTitleId())) {
            response.setCode(ResultCode.PARAMETERS_EMPTY);
            response.setMessage("传入参数不能为空");
            return response;
        }
        List<EditorSkipLogic> editorSkipLogicResList = editorSkipLogicApi.list(req);
        response.setData(editorSkipLogicResList);
        return response;
    }

}
